const defineDirective = (app) => {
    app.directive('longpress', {
        mounted(el, binding) {
            // 定义长按的时间阈值，例如 1000 毫秒
            let pressTimer = null;
            const delay = 1000; // 长按时间

            // 手指触摸时触发
            function startPress() {
                pressTimer = setTimeout(() => {
                    // 执行长按回调
                    binding.value();
                }, delay);
            }

            // 手指离开时触发
            function endPress() {
                clearTimeout(pressTimer);
            }

            // 手指在元素上滑动时触发
            function cancelPress() {
                clearTimeout(pressTimer);
            }

            // 绑定触摸事件
            el.addEventListener('touchstart', startPress);
            el.addEventListener('touchend', endPress);
            el.addEventListener('touchmove', cancelPress);

            // 在元素销毁时解绑事件
            el.__longpressEvents__ = { startPress, endPress, cancelPress };
        },
        beforeUnmount(el, binding) {
            // 解绑之前绑定的事件
            const events = el.__longpressEvents__;
            el.removeEventListener('touchstart', events.startPress);
            el.removeEventListener('touchend', events.endPress);
            el.removeEventListener('touchmove', events.cancelPress);
        }
    });
}

export default defineDirective